# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：batch_tv.py
#日期：2021-06-12
#备注：视频网站下载的电视，电影 ts小片段，进行批量截取图片，然后选择需要的图片合成视频瞬间
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import os
import threading
import math
from pycacho.cachobase.file_rename import rename_num_files,copy_rename_files
from pycacho.cachobase.files_select import get_files,get_all_files
from pythonlession.recordmodule.ffmpeg_model import get_info,get_image

from pycacho.cachobase.logger import Logger
logger = Logger("batch_tv").get_log()


# 目录下（已经选择电视集数的所有视频片段）按帧速率分解成图片：
# video_path-->视频目录；season-->电视集数；type-->视频格式；out_path-->图片分解目录
def videos_image(video_path,season,type,out_path):
    s_video_path = video_path+season+'\\'
    info = get_files(s_video_path,type)
    for file in info:
        file_name = os.path.basename(file)
        if file_name>'02.mp4':
            time = math.ceil(float(get_info(file)['streams'][0]['duration']))
            out_name= season+'_'+file_name.replace('.mp4','')+'_%03d'          # 02_001_001
            image_path = out_path + season + '\\'+'image'+'\\'      # F:\image\baisui\02\image
            if not os.path.exists(image_path):
                os.makedirs(image_path)
            get_image(file, image_path, time,out_name)


# 一个视频进行处理
def one_video_image(video):
    image_path = 'F:\image\\temp\\'
    file_name = os.path.basename(video)
    if file_name > '008.ts':
        time = math.ceil(float(get_info(video)['streams'][0]['duration']))
        image_name = '01_' + '%03d' % (int(file_name.replace('.ts', '')))
        out_path = image_path + image_name + '\\'
        if not os.path.exists(out_path):
            os.makedirs(out_path)
        get_image(video, out_path, time,'')
        print(file_name, out_path, time, image_name)


def threading_video_image(video_path):
    info = get_files(video_path, 'ts')
    threads = [threading.Thread(target=one_video_image, args=(video,)) for video in info]
    for t in threads:
        t.start()

    for t in threads:
        t.join()


if __name__ == '__main__':
    videos_image('F:\image\\lkx\\','01','mp4','F:\image\\lkx\\')

